package com.wyl.salary.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.wyl.salary.pojo.SalaryBean
import com.wyl.salary.ui.theme.SalaryTheme
import com.wyl.salary.utils.format

@Composable
fun SalaryList(
    salaryList: List<SalaryBean>,
    salary: Double,
    duration: Double,
    tipping: Double,
    onClickItem: (sid: Long) -> Unit,
) {
    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
    ) {
        item {
            CountItem(salary, duration, tipping)
        }
        item {
            SalaryItem("日期", "时长", "外卖")
        }
        items(salaryList.size) {
            SalaryItem(
                salaryList[it].date.format(),
                salaryList[it].hours.toString(),
                salaryList[it].tipping.toString(),
                modifier = Modifier
                    .background(
                        if (salaryList[it].remarks.isEmpty()) {
                            Color.Transparent
                        } else {
                            MaterialTheme.colors.secondary.copy(alpha = 0.2f)
                        }
                    )
                    .clickable { onClickItem(salaryList[it].id) }
            )
            if (it != salaryList.lastIndex) {
                Divider()
            }
        }
    }
}


@Preview(showBackground = true)
@Composable
fun PreviewSalaryList() {
    SalaryTheme {
        val data2 = listOf(
            SalaryBean(hours = 8.0, salary = 15.0, tipping = 6.2),
            SalaryBean(hours = 8.0, salary = 15.0, tipping = 6.2),
            SalaryBean(hours = 8.0, salary = 15.0, tipping = 6.2),
            SalaryBean(hours = 8.0, salary = 15.0, tipping = 6.2),
            SalaryBean(hours = 8.0, salary = 15.0, tipping = 6.2),
        )
        SalaryList(data2, salary = 13640.0, tipping = 306.0, duration = 40.0) {}
        /*val data = SalaryBean(
            hours = 8f,
            salary = 15f,
            tipping = 6.2f
        )
        SalaryItem(
            data.date.format(),
            data.hours.toString(),
            data.tipping.toString()
        )*/
    }
}

@Composable
private fun SalaryItem(
    date: String,
    hours: String,
    tipping: String,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(vertical = 8.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(
            text = date, modifier = Modifier
                .weight(1f)
                .wrapContentWidth(Alignment.Start)
        )
        Text(
            text = hours,
            modifier = Modifier
                .weight(1f)
                .wrapContentWidth()
        )
        Text(
            text = tipping,
            modifier = Modifier
                .weight(1f)
                .wrapContentWidth(Alignment.End)
        )
    }
}

@Composable
private fun CountItem(
    salary: Double,
    duration: Double,
    tipping: Double
) {
    Text(
        "上班(${
            String.format("%.1f", duration)
        })：${
            String.format("%.1f", salary)
        }，外卖：${
            String.format("%.1f", tipping)
        } \n共计：${
            String.format("%.1f", salary + tipping)
        }",
        color = MaterialTheme.colors.primary
    )
}